package agh.tk.markuptranslation.gui;

import java.awt.event.ActionEvent;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;

import javax.swing.AbstractAction;

public class SaveAction extends AbstractAction {
	public SaveAction() {
		putValue(NAME, "Save");
	}
	
	public static void saveToFile(String s, File file) {
		BufferedWriter bw = null;
		try {
			bw = new BufferedWriter(new FileWriter(file));
			bw.write(s);
		} catch(Exception ex) {
			ex.printStackTrace(System.err);
		} finally {
			try{
				bw.close();
			} catch(Exception ex) { }
		}
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		File file = Main.getFile();
		if(file == null) {
			SaveAsAction.saveAs();
			return;
		}
		String text = Main.getText();
		saveToFile(text,file);
	}
}
